package com.example.fruitdemo.ExceptionHander;



import com.example.fruitdemo.ruslet.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;


@ControllerAdvice(annotations = {RestController.class, Controller.class})
@Slf4j
@ResponseBody
public class GlobalExceptionHandler {
    //账号不存在异常
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> restcontrollerex(SQLIntegrityConstraintViolationException ex){
        log.error(ex.getMessage());
        if(ex.getMessage().contains("Duplicate entry")) {
            String[] s = ex.getMessage().split(" ");
            String s1 = s[2];
            return R.Err(s1+"已经存在!");
        }
        return R.Err("未知错误！");
    }
    //分类删除关联菜品套餐异常
//    @ExceptionHandler(CustomException.class)
//    public R<String> deleteCategory(CustomException ex){
//        log.error(ex.getMessage());
//
//        return R.error(ex.getMessage());
//    }
    @ExceptionHandler(NullPointerException.class)
    public R<String> nullusername(NullPointerException ex){
        return R.Err("用户名不存在!");
    }
}